<template>
  <div class="">
    <el-tree
      ref="tree"
      :data="permissionData"
      show-checkbox
      node-key="id"
      :props="defaultProps"
      default-expand-all
      check-strictly
      highlight-current
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="handleSubmit">确定</el-button>
      <el-button @click="$emit('update:show',false)">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { handleTreeArray } from '@/utils'
import { assignPerm } from '@/api/settings'
export default {
  name: 'AssignPermission',
  components: {},
  directives: {},
  props: {
    permission: {
      type: Object,
      default: _ => ({})
    },
    show: {
      type: Boolean
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      permissionData: [] // 存储权限数据
    }
  },
  computed: {},
  watch: {},
  async created() {
    this.loadPermissionList()
  },
  mounted() {},
  activated() {},
  updated() {},
  methods: {
    async loadPermissionList() {
      const { data } = await getPermissionList()
      this.permissionData = handleTreeArray(data)
      this.$refs.tree.setCheckedKeys(this.permission.permIds)
    },
    async handleSubmit() {
      await assignPerm({ id: this.permission.id, permIds: this.$refs.tree.getCheckedKeys() })
      this.$emit('update:show', false)
    }
  }
}
</script>

<style scoped lang=''>
</style>
